

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

//一元谓词 应用举例：find_if
class mycompare {

    public:
        bool operator()(int v){
            if (v > 7){
                return true;
            }
            else{
                return false;
            }
        }
};

void test_case1() {
 
    vector<int> v;

    for (int i = 0; i < 10; i++){
        v.push_back(i);
    }
    vector<int>::iterator pos = find_if(v.begin(), v.end(), mycompare()); //匿名函数对象
    if(pos == v.end()){
        cout << "没找到" << endl;
    }
    else{
        cout << "找到:" << *pos << endl;
    }
};



//二元函数对象 应用举例 : transform
class myplus{

    public:
        myplus() { cout << "myplus" <<endl;}
        int operator()(int v1,int v2){
            return v1 + v2;
        }
    
};

void test_case2() { 

    vector<int> v1,v2,v3;
    
    for (int i = 0; i < 10; i++){
        v1.push_back(i);
        v2.push_back(i + 1);
        v3.push_back(0);
    }

    int i = myplus{}(1,3);
    cout << "i = " << i << endl;

    transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), myplus()); //

    for (auto item : v3)
       std::cout << item << std::endl;

}

void test_case3() {

    vector<int> v1,v2,v3;
    
    for (int i = 0; i < 10; i++){
        v1.push_back(i);
        v2.push_back(i + 1);
        v3.push_back(0);
    }

    int i = myplus{}(1,3);
    cout << "i = " << i << endl;

    myplus myplus1;

    transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), myplus1); //

    for (auto item : v3)
       std::cout << item << std::endl;

}

int main()  {

    test_case1();
    test_case2();
    test_case3();

    return 0;
}